package org.dromara.system.controller.device;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.IdsBo;
import org.dromara.system.domain.bo.NameBo;
import org.dromara.system.domain.bo.ValueDescBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.VariableAssociationVo;
import org.dromara.system.domain.bo.VariableAssociationBo;
import org.dromara.system.service.IVariableAssociationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 变量
 *
 * @author Lion Li
 * @date 2024-10-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/association")
public class VariableAssociationController extends BaseController {

    private final IVariableAssociationService variableAssociationService;

    /**
     * 查询变量列表
     */
    @SaCheckPermission("system:association:list")
    @GetMapping("/list")
    public TableDataInfo<VariableAssociationVo> list(VariableAssociationBo bo, PageQuery pageQuery) {
        return variableAssociationService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取变量详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:association:list")
    @GetMapping("/{id}")
    public R<VariableAssociationVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(variableAssociationService.queryById(id));
    }

    /**
     * 新增变量
     */
    @SaCheckPermission("system:association:list")
    @Log(title = "变量", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VariableAssociationBo bo) {
        return toAjax(variableAssociationService.insertByBo(bo));
    }

    /**
     * 修改变量
     */
    @SaCheckPermission("system:association:list")
    @Log(title = "变量", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody VariableAssociationBo bo) {
        return toAjax(variableAssociationService.updateByBo(bo));
    }

    /**
     * 删除变量
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:association:list")
    @Log(title = "变量", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(variableAssociationService.deleteWithValidByIds(List.of(ids), true));
    }


    @SaCheckPermission("system:association:list")
    @Log(title = "批量更新变量分组", businessType = BusinessType.OTHER)
    @RepeatSubmit()
    @PostMapping("/batchGroup/{groupId}")
    public R<Void> updateVariableAssociationGroup(@PathVariable Long groupId, @RequestBody IdsBo bo) {
        return toAjax(variableAssociationService.updateVariableAssociationGroup(groupId, List.of(bo.getIds())));
    }


    @SaCheckPermission("system:association:list")
    @Log(title = "更新变量别名", businessType = BusinessType.OTHER)
    @RepeatSubmit()
    @PutMapping("/updateVariableAliasName/{id}")
    public R<Void> updateVariableAssociationName(@PathVariable Long id, @Validated @RequestBody NameBo bo) {
        return toAjax(variableAssociationService.updateVariableAssociationName(id, bo.getAlias()));
    }

    @SaCheckPermission("system:association:list")
    @Log(title = "保存变量值描述别名", businessType = BusinessType.OTHER)
    @RepeatSubmit()
    @PutMapping("/setVariableAssociationValueDesc/{id}")
    public R<Void> setVariableAssociationValueDesc(@PathVariable Long id, @Validated @RequestBody ValueDescBo bo) {
        return toAjax(variableAssociationService.setVariableAssociationValueDesc(id, bo));
    }

}
